
package AcceptanceTests;
import org.junit.Before;
import org.junit.Test;

import forum.server.exceptions.DatabaseException;
import junit.framework.TestCase;

public class AddPostAcceptanceTest extends TestCase{
	private Bridge br;
	private String username;
	private String pass;

	/**
	 * post conditions for the add post use case is that the user is logged in.
	 */
    @Before
	@Override
    public void setUp(){
		this.br=new Bridge();
		this.username="new_user";
		this.pass="4589";
		this.br.register(this.username,this.pass);
		//register also logges in to the system.
    }

    /**
	 * This function tests the add post use case.
	 * the test covers adding 2 posts.
     * @throws DatabaseException 
	 */
    @Test
    public void testAddPostUseCase() throws DatabaseException{
    	this.br.login(this.username, this.pass);
		int currentPostsCount=this.br.getPostsCount();
		String content="testing - adding a new post.";
		assertTrue(this.br.addPost(content));
		currentPostsCount++;
		int expectedResult=currentPostsCount;
		int result=this.br.getPostsCount();
		//checking that the system was added a new post:
		assertEquals(expectedResult,result);
		//now checking the last post that was added:
		assertEquals(this.br.getAuthorOfLastPostAdded(),this.username);
		assertEquals(this.br.getContentOfLastPostAdded(),content);

		
		//checking again to see that there is no limitation on adding new posts:
		content="testing - adding a another new post.";
		assertTrue(this.br.addPost(content));
		currentPostsCount++;
		expectedResult=currentPostsCount;
		result=this.br.getPostsCount();
		assertEquals(expectedResult,result);
		assertEquals(this.br.getAuthorOfLastPostAdded(),this.username);
		assertEquals(this.br.getContentOfLastPostAdded(),content);

		//now checking that you must be logged in before adding a new post:
		this.br.logoff(username);
		content="testing - not adding a new post.";
		assertFalse(this.br.addPost(content));
    }
	
}
